
package com.wyzz.hr.action;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.eclipse.birt.report.model.api.util.StringUtil;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.Constants;
import com.wyzz.hr.bo.IBenefitTypeBO;
import com.wyzz.hr.bo.IEmpTypeBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.ILocationBO;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.SysConfigManager;

public class EditEmpInit extends BaseAction implements Constants {

	public EditEmpInit() {
		dbConfigManager = DatabaseSysConfigManager.getInstance();
	}

	public String createInit() throws Exception {
		IEmpTypeBO bo = (IEmpTypeBO) SpringBeanFactory.getBean("emptypeBO");
		empTypes = bo.FindEnabledEmpType();
		ILocationBO localbo = (ILocationBO) SpringBeanFactory
				.getBean("locationBO");
		locations = localbo.FindEnabledLocation();
		localbo = null;
		IBenefitTypeBO benefitTypeBo = (IBenefitTypeBO) SpringBeanFactory
				.getBean("benefitTypeBo");
		benefitTypes = benefitTypeBo.findAll();
		exShiftEnable = dbConfigManager.getProperty("sys.examin.shift.enable")
				.trim();
		return "success";
	}

	public String updateInit() throws Exception {
		if (StringUtil.isEmpty(empNo) || empNo.equals("null"))
			empNo = getCurrentEmpNo();
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		String fetch[] = { Employee.PROP_EMP_TYPE, Employee.PROP_EMP_PB_NO,
				Employee.PROP_EMP_DEPT_NO, Employee.PROP_EMP_LOCATION_NO,
				Employee.PROP_EMP_BENEFIT_TYPE };
		emp = empBo.loadEmp(empNo, fetch);
		empName = emp.getEmpName();
		if (!empNo.equals(getCurrentEmpNo())) {
			if ("OWN".equals(super.authorityCondition))
				return "noauth";
			if (("SUB".equals(super.authorityCondition) || "HRSUB"
					.equals(super.authorityCondition))
					&& !checkEmpInCharge(emp, 1))
				return "noauth";
		}
		String empMsn = emp.getEmpMsn();
		if (empMsn != null && !"".equals(empMsn))
			if (empMsn.startsWith("QQ:")) {
				connectionType = "1";
				connectionNo = empMsn.replace("QQ:", "");
			} else if (empMsn.startsWith("WW:")) {
				connectionType = "2";
				connectionNo = empMsn.replace("WW:", "");
			} else {
				connectionType = "0";
				connectionNo = empMsn;
			}
		exShiftEnable = dbConfigManager.getProperty("sys.examin.shift.enable")
				.trim();
		ISysLogBO logBO = (ISysLogBO) SpringBeanFactory.getBean("logBO");
		List tempLogList = logBO.getLogs("employee", empNo);
		getEmp().setLogs(tempLogList);
		emp.setWorkYear(MyTools.getWorkYearProfile(emp.getEmpWorkDate(),
				new Date()));
		emp.setJoinYear(MyTools.getWorkYearProfile(emp.getEmpJoinDate(),
				new Date()));
		IPositionBo posBo = (IPositionBo) SpringBeanFactory
				.getBean("positionBo");
		Position empPos = posBo.getPosByEmpNo(emp.getId(),
				new String[] { Position.PROP_POSITION_PB_ID });
		emp.setPosition(empPos);
		if (super.authorityCondition.equals("HR")
				|| super.authorityCondition.equals("HRSUB")) {
			empTypes = getDrillDown("EmpType", new String[0]);
			locations = getDrillDown("Location", new String[0]);
			benefitTypes = getDrillDown("BenefitType", new String[0]);
			if (StringUtils.isNotEmpty(createFlag))
				addSuccessInfo((new StringBuilder())
						.append("\u5458\u5DE5")
						.append(emp.getEmpName())
						.append(
								"\u521B\u5EFA\u6210\u529F\uFF0C\u60A8\u53EF\u4EE5\u7EE7\u7EED\u4FEE\u6539\u8BE6\u7EC6\u8D44\u6599\u3002")
						.toString());
			return "success";
		} else {
			return "general";
		}
	}

	public String getYearOrMonth(Float joinYear) {
		int value = 0;
		String result = "";
		if (joinYear.floatValue() >= 1.0F) {
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u5E74").toString();
		} else {
			joinYear = Float.valueOf(joinYear.floatValue() * 100F);
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u4E2A\u6708").toString();
		}
		return result;
	}

	public String selfInit() throws Exception {
		return updateInit();
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public List getEmpTypes() {
		return empTypes;
	}

	public void setEmpTypes(List empTypes) {
		this.empTypes = empTypes;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public String getConnectionType() {
		return connectionType;
	}

	public void setConnectionType(String connectionType) {
		this.connectionType = connectionType;
	}

	public String getConnectionNo() {
		return connectionNo;
	}

	public void setConnectionNo(String connectionNo) {
		this.connectionNo = connectionNo;
	}

	public List getBenefitTypes() {
		return benefitTypes;
	}

	public void setBenefitTypes(List benefitTypes) {
		this.benefitTypes = benefitTypes;
	}

	public String getCreateFlag() {
		return createFlag;
	}

	public void setCreateFlag(String createFlag) {
		this.createFlag = createFlag;
	}

	public String getExShiftEnable() {
		return exShiftEnable;
	}

	public void setExShiftEnable(String exShiftEnable) {
		this.exShiftEnable = exShiftEnable;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	private static final long serialVersionUID = 0xfffffffef48c32fdL;
	SysConfigManager dbConfigManager;
	private Employee emp;
	private String connectionType;
	private String connectionNo;
	private List locations;
	private List empTypes;
	private List benefitTypes;
	String exShiftEnable;
	private String createFlag;
	private String empNo;
	private String empName;
}
